package cn.xzhang.boot.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.xzhang.boot.common.pojo.CommonResult;
import cn.xzhang.boot.common.pojo.PageResult;
import cn.xzhang.boot.constant.UserConstant;
import cn.xzhang.boot.model.dto.wallpaperCategories.WallpaperCategoriesAddReqDTO;
import cn.xzhang.boot.model.dto.wallpaperCategories.WallpaperCategoriesPageReqDTO;
import cn.xzhang.boot.model.dto.wallpaperCategories.WallpaperCategoriesStatusReqDTO;
import cn.xzhang.boot.model.dto.wallpaperCategories.WallpaperCategoriesUpdateReqDTO;
import cn.xzhang.boot.model.entity.WallpaperCategories;
import cn.xzhang.boot.model.vo.wallpaperCategories.WallpaperCategoriesVo;
import cn.xzhang.boot.model.vo.wallpaperCategories.WallpaperCategoriesSimpleVo;
import cn.xzhang.boot.service.WallpaperCategoriesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.xzhang.boot.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST_PARAMS;

/**
 * 壁纸分类管理
 *
 * @author <a href="https://github.com/XiaoZhangCode">XiaoZhangCode</a>
 */
@Tag(name = "管理后台 - 壁纸分类管理")
@RestController
@RequestMapping("/wallpaperCategories")
public class WallpaperCategoriesController {

    @Resource
    private WallpaperCategoriesService wallpaperCategoriesService;

    /**
     * 创建WallpaperCategories
     *
     * @param wallpaperCategoriesReqDTO WallpaperCategories添加请求数据传输对象，包含新增WallpaperCategories的信息
     * @return 返回操作结果，其中包含新添加WallpaperCategories的ID
     */
    @PostMapping("/add")
    @Operation(summary = "创建壁纸分类")
    @SaCheckRole(UserConstant.ADMIN_ROLE)
    public CommonResult<Long> addWallpaperCategories(@RequestBody WallpaperCategoriesAddReqDTO wallpaperCategoriesReqDTO) {
        if (wallpaperCategoriesReqDTO == null) {
            return CommonResult.error(BAD_REQUEST_PARAMS);
        }
        // 调用服务层方法，添加，并获取添加结果
        long result = wallpaperCategoriesService.addWallpaperCategories(wallpaperCategoriesReqDTO);
        // 返回添加成功响应结果
        return CommonResult.success(result);
    }

    @PutMapping("/update")
    @Operation(summary = "更新壁纸分类信息")
    @SaCheckRole(UserConstant.ADMIN_ROLE)
    public CommonResult<Boolean> updateWallpaperCategories(@RequestBody @Valid WallpaperCategoriesUpdateReqDTO wallpaperCategoriesReqDTO) {
        // 检查传入的请求数据是否为空
        if (wallpaperCategoriesReqDTO == null) {
            return CommonResult.error(BAD_REQUEST_PARAMS);
        }
        // 调用服务层方法，更新信息，并获取更新结果
        boolean result = wallpaperCategoriesService.updateWallpaperCategories(wallpaperCategoriesReqDTO);
        // 返回更新信息成功响应结果
        return CommonResult.success(result);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除壁纸分类")
    @SaCheckRole(UserConstant.ADMIN_ROLE)
    @Parameter(name = "id", description = "壁纸分类ID",required = true)
    public CommonResult<Boolean> deleteWallpaperCategories(@RequestParam("id") Long id) {
        // 检查传入的ID是否为空
        if (id == null) {
            return CommonResult.error(BAD_REQUEST_PARAMS);
        }
        // 调用服务层方法，删除
        boolean result = wallpaperCategoriesService.deleteWallpaperCategories(id);
        // 返回删除成功响应结果
        return CommonResult.success(result);
    }

    @GetMapping("/get")
    @Operation(summary = "获取壁纸分类")
    @Parameter(name = "id", description = "壁纸分类ID",required = true)
    @SaCheckRole(UserConstant.ADMIN_ROLE)
    public CommonResult<WallpaperCategoriesVo> getWallpaperCategories(@RequestParam("id") Long id) {
        // 检查传入的ID是否为空
        if (id == null) {
            return CommonResult.error(BAD_REQUEST_PARAMS);
        }
        // 调用服务层方法，获取信息，并返回结果
        return CommonResult.success(wallpaperCategoriesService.getWallpaperCategoriesVO(wallpaperCategoriesService.getById(id)));
    }

    @GetMapping("/get/vo")
    @Operation(summary = "获取壁纸分类简要信息")
    @Parameter(name = "id", description = "壁纸分类ID",required = true)
    public CommonResult<WallpaperCategoriesSimpleVo> getWallpaperCategoriesVO(@RequestParam("id") Long id) {
        // 检查传入的ID是否为空
        if (id == null) {
            return CommonResult.error(BAD_REQUEST_PARAMS);
        }
        WallpaperCategories wallpaperCategories = wallpaperCategoriesService.getById(id);
        // 调用服务层方法，获取信息，并返回结果
        return CommonResult.success(wallpaperCategoriesService.getSimpleWallpaperCategoriesVO(wallpaperCategories));
    }

    @GetMapping("/page")
    @Operation(summary = "分页获取壁纸分类列表")
    @SaCheckRole(UserConstant.ADMIN_ROLE)
    public CommonResult<PageResult<WallpaperCategoriesVo>> getWallpaperCategoriesPage(WallpaperCategoriesPageReqDTO wallpaperCategoriesPageReqDTO) {
        // 调用服务层方法，获取分页信息，并返回结果
        return CommonResult.success(wallpaperCategoriesService.getWallpaperCategoriesPage(wallpaperCategoriesPageReqDTO));
    }

    @PostMapping("/update/enabled")
    @Operation(summary = "修改壁纸分类启用状态")
    @SaCheckRole(UserConstant.ADMIN_ROLE)
    public CommonResult<Boolean> updateWallpaperCategoriesEnabled(@RequestBody @Valid WallpaperCategoriesStatusReqDTO wallpaperCategoriesEnabledReqDTO) {
        // 调用服务层方法，修改启用状态，并返回结果
        return CommonResult.success(wallpaperCategoriesService.updateWallpaperCategoriesEnabled(wallpaperCategoriesEnabledReqDTO));
    }

    @PostMapping("/update/selected")
    @Operation(summary = "修改壁纸分类是否为精选")
    @SaCheckRole(UserConstant.ADMIN_ROLE)
    public CommonResult<Boolean> updateWallpaperCategoriesSelected(@RequestBody @Valid WallpaperCategoriesStatusReqDTO wallpaperCategoriesSelectedReqDTO) {
        // 调用服务层方法，修改是否为精选，并返回结果
        return CommonResult.success(wallpaperCategoriesService.updateWallpaperCategoriesSelected(wallpaperCategoriesSelectedReqDTO));
    }

}